package city.spring.modules.cardinfo.controller;

import city.spring.modules.cardinfo.entity.CardEmployeeEntity;
import city.spring.modules.cardinfo.service.CardEmployeeService;
import city.spring.modules.cardinfo.vo.CardEmployeeEntityVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：卡号信息
 *
 * @author pxm
 * @date 2020-12-25 09:18:46
 */
@RestController
@RequestMapping("/card_employee")
public class CardEmployeeController extends ApiController {
    private final CardEmployeeService cardEmployeeService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<CardEmployeeEntity, ?>> entityOrderFields;

    public CardEmployeeController(CardEmployeeService cardEmployeeService) {
        this.cardEmployeeService = cardEmployeeService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(CardEmployeeEntity::getId);
        entityOrderFields.add(CardEmployeeEntity::getMemberCard);
        entityOrderFields.add(CardEmployeeEntity::getLastPhysicsCard);
        entityOrderFields.add(CardEmployeeEntity::getPhysicsCard);
        entityOrderFields.add(CardEmployeeEntity::getPerson);
        entityOrderFields.add(CardEmployeeEntity::getPersonType);
        entityOrderFields.add(CardEmployeeEntity::getOpeningTime);
        entityOrderFields.add(CardEmployeeEntity::getBalance);
        entityOrderFields.add(CardEmployeeEntity::getDepositBalance);
        entityOrderFields.add(CardEmployeeEntity::getStates);
        entityOrderFields.add(CardEmployeeEntity::getUsed);
        entityOrderFields.add(CardEmployeeEntity::getTotalConsumption);
        entityOrderFields.add(CardEmployeeEntity::getLastConsumption);
        entityOrderFields.add(CardEmployeeEntity::getGmtCreate);
        entityOrderFields.add(CardEmployeeEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>卡号信息</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, CardEmployeeEntity entity) {
        LambdaQueryChainWrapper<CardEmployeeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>卡号信息</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, CardEmployeeEntityVO entity) {
        QueryWrapper<? extends CardEmployeeEntity> queryWrapper = buildQuery(entity);
        return success(cardEmployeeService.staffListPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>卡号信息</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        Page<CardEmployeeEntityVO> page = new Page<>();
        QueryWrapper<CardEmployeeEntityVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        IPage<CardEmployeeEntityVO> iPage = cardEmployeeService.staffListPage(page, wrapper);
        List<CardEmployeeEntityVO> records = iPage.getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong>卡号信息</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody CardEmployeeEntity entity) {
        cardEmployeeService.saveCardEmployee(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改物理卡号
     *
     * @param id
     * @param entity
     * @return
     */
    @PutMapping("/editPhysics/{id}")
    public Object editPhysics(@PathVariable String id, @RequestBody CardEmployeeEntity entity) {
        entity.setId(id);
        cardEmployeeService.updatePhysics(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 修改一个 <strong>卡号信息</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody CardEmployeeEntity entity) {
        entity.setId(id);
        cardEmployeeService.updateCardEmployee(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>卡号信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        cardEmployeeService.deleteCardEmployee(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>卡号信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        cardEmployeeService.deleteCardEmployee(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<CardEmployeeEntity> buildLambdaQuery(CardEmployeeEntity entity) {
        LambdaQueryChainWrapper<CardEmployeeEntity> lambdaQuery = cardEmployeeService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), CardEmployeeEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMemberCard()), CardEmployeeEntity::getMemberCard, entity.getMemberCard());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getLastPhysicsCard()), CardEmployeeEntity::getLastPhysicsCard, entity.getLastPhysicsCard());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhysicsCard()), CardEmployeeEntity::getPhysicsCard, entity.getPhysicsCard());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPerson()), CardEmployeeEntity::getPerson, entity.getPerson());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPersonType()), CardEmployeeEntity::getPersonType, entity.getPersonType());
        lambdaQuery.eq(entity.getOpeningTime() != null, CardEmployeeEntity::getOpeningTime, entity.getOpeningTime());
        lambdaQuery.eq(entity.getBalance() != null, CardEmployeeEntity::getBalance, entity.getBalance());
        lambdaQuery.eq(entity.getDepositBalance() != null, CardEmployeeEntity::getDepositBalance, entity.getDepositBalance());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getStates()), CardEmployeeEntity::getStates, entity.getStates());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getUsed()), CardEmployeeEntity::getUsed, entity.getUsed());
        lambdaQuery.eq(entity.getTotalConsumption() != null, CardEmployeeEntity::getTotalConsumption, entity.getTotalConsumption());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getLastConsumption()), CardEmployeeEntity::getLastConsumption, entity.getLastConsumption());
        lambdaQuery.eq(entity.getGmtCreate() != null, CardEmployeeEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, CardEmployeeEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<? extends CardEmployeeEntity> buildQuery(CardEmployeeEntityVO entity) {
        QueryWrapper<? extends CardEmployeeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getMemberCard()), "t1.member_card", entity.getMemberCard());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonName()), "t2.name", entity.getPersonName());
        wrapper.like(StringUtils.isNotBlank(entity.getLastPhysicsCard()), "t1.last_physics_card", entity.getLastPhysicsCard());
        wrapper.like(StringUtils.isNotBlank(entity.getPhysicsCard()), "t1.physics_card", entity.getPhysicsCard());
        wrapper.like(StringUtils.isNotBlank(entity.getPerson()), "t1.person", entity.getPerson());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonType()), "t1.person_type", entity.getPersonType());
        wrapper.eq(entity.getOpeningTime() != null, "t1.opening_time", entity.getOpeningTime());
        wrapper.eq(entity.getBalance() != null, "t1.balance", entity.getBalance());
        wrapper.eq(entity.getDepositBalance() != null, "t1.deposit_balance", entity.getDepositBalance());
        wrapper.like(StringUtils.isNotBlank(entity.getStates()), "t1.states", entity.getStates());
        wrapper.like(StringUtils.isNotBlank(entity.getUsed()), "t1.used", entity.getUsed());
        wrapper.eq(entity.getTotalConsumption() != null, "t1.total_consumption", entity.getTotalConsumption());
        wrapper.like(StringUtils.isNotBlank(entity.getLastConsumption()), "t1.last_consumption", entity.getLastConsumption());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
